<template>
    <Layout>
        <Table ref='table' :height="tableHeight" :loading="loading" no-data-text="没有查询到相关数据!"
               border :columns="columns1" size="small" :data="dataTable">
        </Table>
        <Row style="margin-top:10px;">
            <Col span="24">
                <Page style='float: right' :total="pagedata.total" :page-size="pagedata.pageSize"
                      @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray"
                      show-total show-sizer/>
            </Col>
        </Row>
    </layout>
</template>

<script>
    import {Api} from '../../../../api/index.js'
    import {sessionStorage} from '../../../../common/cacheUtils.js';
    export default {
        data() {
            return {
				codingSortList:[],
                form1: {},
                //分页参数
                pageSizeArray: [10, 20, 50],
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                },
                columns1: [
                    {
                        title: "序号",
                        width: 80,
                        align: "center",
                        fixed: 'left',
                        render: (h, params) => {
                            return h('span', params.index + (this.pagedata.currentPage - 1) * this.pagedata.pageSize + 1);
                        }
                    },
                    {
                        title: '预案名称',
                        align: 'center',
                        minWidth: 100,
                        key: 'emName',
                    },
                    {
                        title: '预案级别',
                        width: 150,
                        align: 'center',
                        key: 'emLevelName',
                    	render: (h, params) =>{
                    	    return h('span', this.getCodingSortName(params.row.emLevelId));
                    	}
                    },
                    {
                        title: '事件类型',
                        width: 150,
                        align: 'center',
                        key: 'eventTypeName',
                    	render: (h, params) =>{
                    	    return h('span', this.getCodingSortName(params.row.eventTypeId));
                    	}
                    },
                    {
                        title: '预案类别',
                        width: 150,
                        align: 'center',
                        key: 'emTypeName',
                    	render: (h, params) =>{
                    	    return h('span', this.getCodingSortName(params.row.emType));
                    	}
                    },
                    {
                        title: '编制日期',
                        align: 'center',
                        key: 'releaseDate',
                        minWidth: 100,
                        render: (h, params) => {
                            return h('div', [
                                (this.formatDate(new Date(params.row.releaseDate), 'yyyy-MM-dd'))
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                            this.save(params.row);
                                        }
                                    }
                                }, '选择')
                            ]);
                        }
                    }

                ],
                tableHeight: 450,
                loading: false,
                dataTable: [],
            }
        },
        mounted: function () {
            this.tableHeight = window.innerHeight * 0.85 - this.$refs.table.$el.offsetTop - 73;
            this.selectCodingSort();
			this.query();
        },
        methods: {
			pageChange(pageNum) {
			    this.pagedata.currentPage = pageNum;
			    this.query();
			},
			pageSizeChange(pageSize) {
			    this.pagedata.pageSize = pageSize;
			    this.query();
			},

            copyArray(arr) {
                return JSON.parse(JSON.stringify(arr));
            },

            save(row) {
                this.$emit('getSelectData', row);
            },
            query() {
                this.loading = true;
                let data = {
                    startPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize,
                    orgId: sessionStorage('userId').getObject('userId').companyId,
                    type:this.$route.query.id
                };
                data = Object.assign(data, JSON.parse(JSON.stringify(this.form1)));
                			
                let url = Api.queryEmergencyMeasure;
                this.$http.get(url, {params: data}).then((res) => {
                    this.dataTable = res.data.data.data;
                    this.pagedata.total = res.data.data.totalSize;
                    this.loading = false;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            reset() {
                this.form1 = {}
            },
            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },
			selectCodingSort() {
			    this.$http.get(Api.queryCodeDetailBySortId + '/PL-000001', {params: {}}).then((res) => {
					this.emLevelList = res.data.data;
					this.codingSortList = this.codingSortList.concat(this.emLevelList);
			    });
			    this.$http.get(Api.queryCodeDetailBySortId + '/ET-000001', {params: {}}).then((res) => {
			        this.eventTypeList = res.data.data;
					this.codingSortList = this.codingSortList.concat(this.eventTypeList);
			    });
			    this.$http.get(Api.queryCodeDetailBySortId + '/PL-000010', {params: {}}).then((res) => {
			        this.emTypeList = res.data.data;
					this.codingSortList = this.codingSortList.concat(this.emTypeList);
			    });
			},
			getCodingSortName(codingCodeId){
				let codingName = ''
				try{
					this.codingSortList.forEach(function(e){
					   if(codingCodeId == e.codingCodeId){
						   codingName = e.codingName
						   throw Error();
					   }
					});
				}catch(e){
				}
				return codingName;
			}
        },
    }
</script>
